<template>
<div class="app-container">
  <!--查询条件表单-->
  <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
    <el-form-item label="年级" prop="gradeId">
      <el-select v-model="queryParams.gradeId" placeholder="年级" clearable>
        <el-option v-for="item in gradeOptions" :key="item.gradeId" :label="item.gradeName" :value="item.gradeId" />
      </el-select>
    </el-form-item>
    <el-form-item label="课程" prop="courseId">
      <el-select v-model="queryParams.courseId" placeholder="课程" clearable>
        <el-option v-for="item in courseOptions" :key="item.courseId" :label="item.courseName" :value="item.courseId" />
      </el-select>
    </el-form-item>
    <el-form-item label="教师" prop="leaderId">
      <el-select v-model="queryParams.leaderId" placeholder="教师" clearable>
        <el-option v-for="item in teacherOptions" :key="item.teacherId" :label="item.teacherName" :value="item.teacherId" />
      </el-select>
    </el-form-item>
    <el-form-item label="状态" prop="status">
      <el-select v-model="queryParams.status" placeholder="状态" clearable>
        <el-option v-for="dict in sys_status" :key="dict.value" :label="dict.label" :value="dict.value" />
      </el-select>
    </el-form-item>
    <el-form-item>
      <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
      <el-button icon="Refresh" @click="resetQuery">重置</el-button>
    </el-form-item>
  </el-form>

  <!--按钮操作区-->
  <el-row :gutter="10" class="mb8">
    <el-col :span="1.5">
      <el-button type="primary" plain icon="Plus" @click="handleAdd" v-hasPermission="['school:grade_course:add']"
      >新增</el-button
      >
    </el-col>
    <el-col :span="1.5">
      <el-button
          type="success"
          plain
          icon="Edit"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermission="['school:grade_course:edit']"
      >修改</el-button
      >
    </el-col>
    <el-col :span="1.5">
      <el-button
          type="danger"
          plain
          icon="Delete"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermission="['school:grade_course:remove']"
      >删除</el-button
      >
    </el-col>
    <el-col :span="1.5">
      <el-button type="warning" plain icon="Download" @click="handleExport" v-hasPermission="['school:grade_course:export']"
      >导出</el-button
      >
    </el-col>
    <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
  </el-row>

  <!--数据表格区-->
  <el-table v-loading="loading" :data="gradeCourseList" @selection-change="handleSelectionChange">
    <el-table-column type="selection" width="55" align="center" />
    <el-table-column label="年课ID" align="center" prop="gradeCourseId" />
    <el-table-column label="年级" align="center" prop="gradeName" />
    <el-table-column label="课程" align="center" prop="courseName" />
    <el-table-column label="学科组长" align="center" prop="leaderName" />
    <el-table-column label="状态" align="center" prop="status">
      <template #default="scope">
        <dict-tag :options="sys_status" :value="scope.row.status" />
      </template>
    </el-table-column>
    <el-table-column label="创建时间" align="center" prop="createTime" width="180">
      <template #default="scope">
        <span>{{ parseTime(scope.row.createTime) }}</span>
      </template>
    </el-table-column>
    <el-table-column label="操作" width="206" align="center" class-name="small-padding fixed-width">
      <template #default="scope">
        <el-button
            link
            type="primary"
            icon="Edit"
            @click="handleUpdate(scope.row)"
            v-hasPermission="['school:grade_course:edit']"
        >修改</el-button
        >
        <el-button
            link
            type="primary"
            icon="Delete"
            @click="handleDelete(scope.row)"
            v-hasPermission="['school:grade_course:remove']"
        >删除</el-button
        >
      </template>
    </el-table-column>
  </el-table>

  <!--表格分页-->
  <pagination
      v-show="total > 0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
  />

  <!-- 添加或修改年级课程对话框 -->
  <el-dialog :title="title" v-model="open" width="500px" append-to-body>
    <el-form ref="gradeCourseRef" :model="form" :rules="rules" label-width="80px">
      <el-form-item label="年级" prop="gradeId">
        <el-select v-model="form.gradeId" placeholder="请选择">
          <el-option
              v-for="item in gradeOptions"
              :key="item.gradeId"
              :label="item.gradeName"
              :value="item.gradeId"
              :disabled="item.status == 0"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="课程" prop="courseId">
        <el-select v-model="form.courseId" placeholder="请选择">
          <el-option
              v-for="item in courseOptions"
              :key="item.courseId"
              :label="item.courseName"
              :value="item.courseId"
              :disabled="item.status == 0"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="学科组长" prop="leaderId">
        <el-select v-model="form.leaderId" placeholder="请选择">
          <el-option
              v-for="item in teacherOptions"
              :key="item.teacherId"
              :label="item.teacherName"
              :value="item.teacherId"
              :disabled="item.status == 0"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-radio-group v-model="form.status">
          <el-radio v-for="dict in sys_status" :key="dict.value" :label="dict.value">{{ dict.label }}</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="备注" prop="remark">
        <el-input v-model="form.remark" type="textarea" placeholder="请输入内容" />
      </el-form-item>
    </el-form>
    <template #footer>
      <div class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </template>
  </el-dialog>

</div>
</template>

<script setup name="GradeCourse">
import { ref, reactive, toRefs, getCurrentInstance } from "vue";
import * as gradeCourseApi from "@/api/school/gradeCourseApi";
import * as publicApi from "@/api/school/publicApi";

/** 公共变量 */
const { proxy } = getCurrentInstance();
const { sys_status } = proxy.useDict('sys_status');
const gradeOptions = ref([]);
const courseOptions = ref([]);
const teacherOptions = ref([]);
/** 年级列表 */
publicApi.allGradeList().then((response) => {
  gradeOptions.value = response
})
/** 课程列表 */
publicApi.allCourseList().then((response) => {
  courseOptions.value = response
})
/** 教师列表 */
publicApi.allTeacherList().then((response) => {
  teacherOptions.value = response
})

const data = reactive({
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    gradeId: undefined,
    courseId: undefined,
    leaderId: undefined,
    status: undefined,
  },
  form: {
    gradeId: undefined,
    courseId: undefined,
    leaderId: undefined,
    status: 1,
    remark: undefined
  },
  rules: {
    gradeId: [{ required: true, message: '年级不能为空', trigger: 'blur' }],
    courseId: [{ required: true, message: '课程不能为空', trigger: 'blur' }],
    leaderId: [{ required: true, message: '学科组长不能为空', trigger: 'blur' }],
    status: [{ required: true, message: '状态不能为空', trigger: 'blur' }],
  }
})

const { queryParams, form, rules } = toRefs(data)

/** 查询表单相关变量 */
const showSearch = ref(true);

/** 表格相关变量*/
const loading = ref(true);
const gradeCourseList = ref([]);
const total = ref(0);

/** 查询年级列表 */
function getList() {
  loading.value = true;
  gradeCourseApi
      .listGradeCourse(queryParams.value)
      .then((response) => {
        gradeCourseList.value = response.rows;
        total.value = response.total;
      })
      .finally(() => {
        loading.value = false;
      });
}
getList();

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm('queryRef');
  handleQuery();
}

/** 按钮操作区相关变量 */
const single = ref(true);
const multiple = ref(true);
const open = ref(false);
const title = ref('');
const ids = ref([]);



/** 表单重置 */
function reset() {
  form.value = {
    gradeCourseId: undefined,
    gradeId: undefined,
    courseId: undefined,
    leaderId: undefined,
    status: 1,
    remark: undefined,
  };
  proxy.resetForm('gradeCourseRef');
}

/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = '添加年级课程';
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset();
  const gradeCourseId = row.gradeCourseId || ids.value;
  gradeCourseApi.getGradeCourse(gradeCourseId).then((response) => {
    form.value = response;
    open.value = true;
    title.value = '修改年级课程';
  });
}


/** 删除按钮操作 */
function handleDelete(row) {
  const gradeCourseIds = row.gradeCourseId || ids.value;
  proxy.$modal
      .confirm(`是否确认删除年级课程ID为"${gradeCourseIds}"的数据项？`)
      .then(() => gradeCourseApi.deleteGradeCourse(gradeCourseIds))
      .then(() => {
        getList();
        proxy.$modal.msgSuccess('删除成功');
      })
      .catch(() => {});
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download(
      'school/grade_course/export',
      {
        ...queryParams.value,
      },
      `grade_course_${new Date().getTime()}.xlsx`,
  );
}


/** 多选框选中数据 */
function handleSelectionChange(selection) {
  ids.value = selection.map((item) => item.gradeCourseId);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}


/** 提交按钮 */
function submitForm() {
  proxy.$refs.gradeCourseRef.validate((valid) => {
    if (valid) {
      if (form.value.gradeCourseId != undefined) {
        gradeCourseApi.updateGradeCourse(form.value).then((response) => {
          proxy.$modal.msgSuccess('修改成功');
          open.value = false;
          getList();
        });
      } else {
        gradeCourseApi.addGradeCourse(form.value).then((response) => {
          proxy.$modal.msgSuccess('新增成功');
          open.value = false;
          getList();
        });
      }
    }
  });
}

/** 取消按钮 */
function cancel() {
  open.value = false;
  reset();
}


</script>

<style scoped>

</style>
